﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;
using System.Collections;

namespace WebMMS.Base
{
    public partial class Timers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    AspNetPager1.CurrentPageIndex = 1;
                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region GV info
        private void GVDataBind()
        {
            string strSQL = "SELECT a.ID,a.PID,a.APPID,a.Timer,a.CreateTime FROM T_Timers a WITH(NOLOCK) WHERE 1=1";
            if (!string.IsNullOrEmpty(txtPID.Text.Trim()))
                strSQL += string.Format(" AND a.PID = '{0}'", txtPID.Text.Trim());
            if (!string.IsNullOrEmpty(txtAPPID.Text.Trim()))
                strSQL += string.Format(" AND a.APPID = '{0}'", txtAPPID.Text.Trim());

            string strOrder = " ORDER BY a.CreateTime DESC";

            AspNetPager1.RecordCount = DbHelperSql.GetSingle("SELECT COUNT(1) FROM (" + strSQL + ") AS B").ToIntOr();

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT ", "SELECT TOP {0} "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT ", string.Format("SELECT row_number() over({0}) rn, ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;
                DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_Timers WHERE ID={0}", strID));
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
                AspNetPager1.CurrentPageIndex = 1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;

                string strName = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtTimer")).Text.Trim();
                if (string.IsNullOrEmpty(strName))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "联网时间间隔不能为空！");
                    return;
                }
                int intTemp = 0;
                try
                {
                    intTemp = Convert.ToInt32(strName);
                    if (intTemp <= 0)
                    {
                        MM.Common.Common.PageStartAlert(this.Page, "联网时间间隔必须为正整数！");
                        return;
                    }

                }
                catch
                {
                    MM.Common.Common.PageStartAlert(this.Page, "联网时间间隔必须为正整数！");
                    return;
                }

                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_Timers SET Timer={0} WHERE ID={1}", strName, strID));
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
            try
            {
                string strIDs = UpdateIDs(hdfBoxValues.Value);
                if (!string.IsNullOrEmpty(strIDs))
                {
                    MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_Timers WHERE ID IN ({0})", strIDs));
                    MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
                    AspNetPager1.CurrentPageIndex = 1;
                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private string UpdateIDs(string IDs)
        {
            if (IDs.Contains(","))
                IDs = IDs.Remove(IDs.LastIndexOf(","));

            return IDs;
        }
        #endregion
    }
}